package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgProjectDataMapper;
import com.csun.dsms.mapper.HgProjectInfoMapper;
import com.csun.dsms.model.huang.HgProjectData;
import com.csun.dsms.model.huang.HgProjectInfo;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgProjectDataService;
import com.csun.dsms.service.HgProjectInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgProjectInfoServiceImpl extends BaseService<HgProjectInfo>
        implements HgProjectInfoService {

    @Resource
    private HgProjectInfoMapper hgProjectInfoMapper;
    
    @Override
    public List<HgProjectInfo> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgProjectInfoMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Wrapper importProjectInfo(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgProjectInfo hgProjectInfo = new HgProjectInfo();
                hgProjectInfo.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgProjectInfo.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgProjectInfo.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgProjectInfo.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgProjectInfo.setIndustryName(Convert.toStr(excelMap.get("行业名称")));
                hgProjectInfo.setProjectName(Convert.toStr(excelMap.get("项目名称")));
                hgProjectInfo.setProjectFund(Convert.toDouble(excelMap.get("项目基金")));
                hgProjectInfo.setGuaranteePerson(Convert.toStr(excelMap.get("担保人")));
                hgProjectInfo.setGuaranteePersonPosition(Convert.toStr(excelMap.get("保证人岗位")));
                hgProjectInfo.setInclusionReason(Convert.toStr(excelMap.get("包含原因")));
                hgProjectInfo.setRedBlackType(Convert.toInt(excelMap.get("红黑类型")));
                hgProjectInfo.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgProjectInfo.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgProjectInfo.setApproved(Convert.toBool(excelMap.get("是否审核")));
                hgProjectInfo.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgProjectInfo.setCreatedTime(new Date());
                hgProjectInfoMapper.insert(hgProjectInfo);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入建设清单成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }


}
